//
//  NewGoodsTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/26.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

typealias BackStringValue = (String)->(Void)

class NewGoodsTableViewCell: UITableViewCell {
    
    var loginClosure:TouchBlock!
    
    
    @IBOutlet weak var leftContentView: UIView!
    @IBOutlet weak var rightContentView: UIView!
    @IBOutlet weak var leftGoodsImg: UIImageView!
    @IBOutlet weak var rightGoodsImg: UIImageView!
    @IBOutlet weak var leftGoodsName: UILabel!
    @IBOutlet weak var rightGoodsName: UILabel!
    @IBOutlet weak var leftNowPrice: UILabel!
    @IBOutlet weak var rightNowPrice: UILabel!
    @IBOutlet weak var leftBazaarPrice: UILabel!
    @IBOutlet weak var rightBazaarPrice: UILabel!
    
    var seleClosure:BackIntValue!
    
    var leftGoodsModel:SiteGoodsModel!
    var rightGoodsModel:SiteGoodsModel!
    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        
        self.corner(byRoundingCorners:  [.topLeft,.topRight], radii: 10, imgV: self.leftGoodsImg)
        self.corner(byRoundingCorners:  [.topLeft,.topRight], radii: 10, imgV: self.rightGoodsImg)
        
        self.leftContentView.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 1.5), radius: 5, opacity: 0.15)
        self.leftContentView.setCornerRadio(radio: 10, isMask: false)
        
        self.rightContentView.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 1.5), radius: 5, opacity: 0.15)
        self.rightContentView.setCornerRadio(radio: 10, isMask: false)
    }
    
    
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat,imgV:UIImageView) {
        let width = (UIScreen.ScreenWidth - 30)/2 - 10
        let height = width/(16/13)
        let bounds = CGRect.init(x: imgV.left, y: imgV.top, width: width, height: height)
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        imgV.layer.mask = maskLayer
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    func reload(withLeftGoods leftGoods:SiteGoodsModel,rightGoods:SiteGoodsModel){
        self.leftGoodsModel = leftGoods
        self.rightGoodsModel = rightGoods
        
        self.leftGoodsImg.my_setImage(url: leftGoods.img, placeholder: Api.placeHolder.rawValue)
        self.leftGoodsName.text = leftGoods.name
        self.rightGoodsImg.my_setImage(url: rightGoods.img, placeholder: Api.placeHolder.rawValue)
        self.rightGoodsName.text = rightGoods.name
        
        var leftPrice = ""
        if leftGoods.isDiscount == 1 {
            leftPrice = "\(leftGoods.discountMoney)"
        }else if leftGoods.isActivity == 1 {
            leftPrice = "\(leftGoods.activityMoney)"
        }else if leftGoods.peddlersPrice != nil {
            leftPrice = leftGoods.peddlersPrice!
        }else{
            leftPrice = "\(leftGoods.price)"
        }
        
        var rightPrice = ""
        if rightGoods.isDiscount == 1 {
            rightPrice = "\(rightGoods.discountMoney)"
        }else if rightGoods.isActivity == 1 {
            rightPrice = "\(rightGoods.activityMoney)"
        }else if rightGoods.peddlersPrice != nil {
            rightPrice = rightGoods.peddlersPrice!
        }else{
            rightPrice = "\(rightGoods.price)"
        }
        
        self.leftNowPrice.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(leftPrice)/斤", changeString: leftPrice.getPriceInteger(), font: UIFont.systemFont(ofSize: 17))
        self.rightNowPrice.attributedText = Utils.changeLabelTextFontSize(normalString: "¥\(rightPrice)/斤", changeString: rightPrice.getPriceInteger(), font: UIFont.systemFont(ofSize: 17))
        self.leftBazaarPrice.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(leftGoods.marketPrice)", deleString: "￥" + "\(leftGoods.marketPrice)")
        self.rightBazaarPrice.attributedText = Utils.labelTextDeleLine(normalString: "市场价：￥\(rightGoods.marketPrice)", deleString: "￥" + "\(rightGoods.marketPrice)")
    }
    
    
    
    // MARK: 点击左边商品
    @IBAction func leftGoodsSele(_ sender: UIButton) {
        self.seleClosure(self.leftGoodsModel.id)
    }
    
    // MARK: 左边商品添加购物车
    @IBAction func LeftAddMethod(_ sender: UIButton) {
        guard Utils.isLogined() else{
            PKhud.showText(text: "请先登录~")
            if self.loginClosure != nil {
                self.loginClosure()
            }
            return
        }
        Utils.insertCart(goodsId: self.leftGoodsModel.id) { (value) in
            if value.0 {
                PKhud.showText(text: "添加成功~")
            }else{
                PKhud.showText(text: value.1)
            }
        }
        
    }
    
    // MARK: 点击右边商品
    @IBAction func rightGoodsSele(_ sender: UIButton) {
        self.seleClosure(self.rightGoodsModel.id)
    }
    
    
    // MARK: 右边商品添加购物车
    @IBAction func rightAddMethod(_ sender: UIButton) {
        guard Utils.isLogined() else{
            PKhud.showText(text: "请先登录~")
            if self.loginClosure != nil {
                self.loginClosure()
            }
            return
        }
        Utils.insertCart(goodsId: self.rightGoodsModel.id) { (value) in
            if value.0 {
                PKhud.showText(text: "添加成功~")
            }else{
                PKhud.showText(text: value.1)
            }
        }
    }
    

}
